﻿//*****************************************************************************
// (C) Copyright 2010 Tiago Pascoal
// This code is licensed under the Microsoft Public License (Ms-PL) 
// [http://www.microsoft.com/opensource/licenses.mspx#Ms-PL].
//
// This code is part of the Work Item Visualization project (
// http://visualization.codeplex.com)
//
//*****************************************************************************


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace TFhisto.UI.Controls.QueryEditorControl
{

    [Flags]
    public enum TrackerOperation { None=0, Move=1, Resize=2};

    /// <summary>
    /// Keeps track of the initial size and location of a control and allows you to move control relativily to the original location
    /// </summary>
    internal class ControlTracker
    {
        private Point _originalLocation;
        private int _originalWidth;
        private Control _control;
        private TrackerOperation _operation;

        /// <summary>
        /// Initializes the tracking of a control
        /// </summary>
        /// <param name="control">The control to be tracked</param>
        public ControlTracker(Control control)
            : this(control, TrackerOperation.None)
        {
        }

        /// <summary>
        /// Initializes the tracking of a control
        /// </summary>
        /// <param name="control">The control to be tracked</param>
        /// <param name="operation">The operation to be performed when DoOperation is called. You can combine more than one operation</param>
        public ControlTracker(Control control,TrackerOperation operation)
        {
            _originalLocation = new Point(control.Location.X, control.Location.Y);
            _control = control;
            _originalWidth = control.Width;
            _operation = operation;
        }        

        /// <summary>
        /// Moves the control (X coordinate) relative to the initial location also resizes the control
        /// (X coordinate) by deltaX points relative to the initial width.
        /// 
        /// <remarks>The current Y location is preserved. Even if it's different from the original Y coordinate</remarks>
        /// 
        /// <example>
        /// If a control initial location is (0,0) and witdh is 345. If we do a MoveAndResizeX(5) the control will be moved 
        /// to (5,0) and the width will be 350 (345+5)
        /// </example>
        /// 
        /// </summary>
        /// <param name="deltaX"></param>
        public void MoveAndResizeX(int deltaX)
        {
            MoveX(deltaX);
            ResizeWidth(deltaX);
        }

        /// <summary>
        /// Moves the control (X coordinate) relative to the initial location 
        /// 
        /// <example>
        /// If a control initial location is (0,0). If we do a MoveX(5) the control will be moved 
        /// to (5,0) 
        /// </example>
        /// </summary>
        /// <param name="deltaX"></param>
        public void MoveX(int deltaX)
        {
            _control.Location = new Point(_originalLocation.X + deltaX, _control.Location.Y);
        }

        /// <summary>
        /// Resizes the control relative to it's initial width.
        /// 
        /// The location is unchanged
        /// 
        /// <example>If a control initial witdh is 345 and we do a ResizeWidth(5) the width will be 350</example>
        /// </summary>
        /// <param name="deltaX"></param>
        public void ResizeWidth(int deltaX)
        {
            _control.Width = _originalWidth + deltaX;
        }

        /// <summary>
        /// Performed the operation that has been defined for this tracked control.
        /// </summary>
        /// <param name="deltaX"></param>
        public void PerformDefinidedOperation(int deltaX)
        {
            if (_operation.HasFlag(TrackerOperation.Move)) MoveX(deltaX);
            if (_operation.HasFlag(TrackerOperation.Resize)) ResizeWidth(deltaX);
        }
    }
}
